package com.sensePlusVR.utils;

import java.util.List;

import android.annotation.SuppressLint;
import android.os.Handler;

import com.sensePlusVR.bean.CollectionItem;
import com.sensePlusVR.bean.GameItem;
import com.sensePlusVR.net.NetRequestManager;
public class DownloadTask{
	private static final String TAG = DownloadTask.class.getSimpleName();
	
	public static final int DownloadDoing = 1;//下载成功
	public static final int DownloadFailed = 2;//下载失败
	public static final int DownloadCancel = 3;//下载取消
	public static final int DownloadPause = 4;//下载暂停
	public static final int DownloadSuccess = 5;//下载完成
	public static final int DownloadRealDoing = 6;//下载
	public static final int DownloadPending = 7;//网络原因的暂停
	
	
	private GameItem gameItem;
	private List<String> urlList;
	private String savePath;
	private int progress = 0;
	private int downloadState = DownloadDoing;
	private boolean cancelDownload = false;
	private boolean waiting ;
	private int reTry;
	private int MaxRetry;
	private boolean mNeedNotifyRealDoing = true;
	private static int downloadSpeed; //KBps
	private long doneLen;
	private boolean allowCelluar;
	private static final int MSG_WATH_SUCCESS = 100;
	
	public void setDoneLen(long len){
		doneLen = len;
	}
	public long getDoneLen(){
		return doneLen;
	}
	
	public static void setSpeed(int speed){
		downloadSpeed = speed;
	}
	
	public static int getSpeed(){
		return downloadSpeed;
	}
	
	public void allowCelluar(boolean allow){
		allowCelluar = allow;
	}
	
	public boolean isCelluarAllowd(){
		return allowCelluar;
	}
	//成功和失败写入数据库
	public void involkManagerCb()
	{
		
		int state ;
		//进度为100，即为下载完成
		if(progress==100){
			
			handler.sendEmptyMessage(MSG_WATH_SUCCESS);
			
			downloadState=DownloadSuccess;
		}
		
		CollectionItem clcItem = CollectionItem.query(gameItem.getId());
		if(clcItem!=null){
			
			if(downloadState==DownloadSuccess){
				
				clcItem.setDownloadState(CollectionItem.DownloadFinished);
				
				CollectionItem.saveOrUpdate(clcItem);
				
			}else if(downloadState==DownloadFailed){
				
				clcItem.setDownloadState(CollectionItem.DownloadError);
				
				CollectionItem.saveOrUpdate(clcItem);
				
			}
		}
		
		if(downloadState==DownloadSuccess){
			
			GameDownload.rmRunningProgress(gameItem.getId());
			
		}else{
			MyLog.d("DownloadTask", "progress : " + progress);
			//设置运行时进度
			GameDownload.setRunningProgresss(gameItem.getId(), progress);
		}
		state = downloadState;
		if (!isWaiting() && mNeedNotifyRealDoing){
			mNeedNotifyRealDoing = false;
			state = DownloadRealDoing;
		}
		MyLog.d(TAG, "gameItem isWaiting : " + isWaiting() + " mNeedNotifyRealDoing : " + mNeedNotifyRealDoing);
		MyLog.d(TAG, "gameItem id : " + gameItem.getId() + " state : " + state);
		
		//回掉	
		GameDownload.involkeGameProgress(gameItem, progress, state);

	}
	
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case MSG_WATH_SUCCESS:
				
				//上传下载次数
				NetRequestManager.addDownloadRecord(gameItem.getId());
				
				break;

			}
			
			
		};
	};
	
	/**
	 * 更新总长度
	 * @param totalLen
	 */
	public void updateTotalLen(long totalLen){
		
		CollectionItem clcItem = CollectionItem.query(gameItem.getId());
		if(clcItem!=null){
			
			clcItem.setTotalLen(totalLen);
			
			CollectionItem.saveOrUpdate(clcItem);
		}
	}
	

	public DownloadTask(GameItem gameItem,String savePath,List<String> urlList){
		this.gameItem = gameItem;
		this.savePath = savePath;
		this.urlList = urlList;
		waiting = true;
		downloadState = DownloadDoing;
		
		
	}
	
	public void reset(){
		downloadState = DownloadDoing;
		waiting = true;
		cancelDownload = false;
	}
	
	public int getGameId(){
		if (gameItem != null)
			return gameItem.getId();
		return -1;
	}
	
	public boolean isWaiting() {
		return waiting;
	}

	public void setWaiting(boolean waiting) {
		this.waiting = waiting;
	}


	public int getProgress(){
		return progress;
	}

	public void setProgress(int progress) {
		this.progress = progress;
	}


	public List<String> getUrlList(){
		return urlList;
	}
	
	public String getSavePath(){
		
		return savePath;
	}
	
	public void CancelTask(){
		cancelDownload = true;
	}
	public boolean Cancelled(){
		return cancelDownload;
	}

	public int getReTry() {
		return reTry;
	}

	public void setReTry(int reTry) {
		this.reTry = reTry;
	}

	public int getMaxRetry() {
		return MaxRetry;
	}

	public void setMaxRetry(int maxRetry) {
		MaxRetry = maxRetry;
	}
	
	
	public int getDownloadState() {
		return downloadState;
	}

	public void setDownloadState(int downloadState) {
		this.downloadState = downloadState;
	}


	
}

